package com.ecoolex.hotel.saas.admin.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.hotel.saas.admin.core.AdminConstant;
import com.ecoolex.hotel.saas.entity.ActivityStore;
import com.ecoolex.hotel.saas.entity.Store;
import com.ecoolex.hotel.saas.ws.ActivityService;
import com.ecoolex.hotel.saas.ws.StoreService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import java.util.List;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "商圈活动内容关联商户管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
        key = ActivityStoreGraphql.AUTH,
        name = "商圈活动内容关联商户管理",
        remark = "商圈活动内容关联商户管理")
public class ActivityStoreGraphql {

    public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-ACTIVITY-STORE";

    private static final String AUTH_GET = AUTH + "-GET";

    private static final String AUTH_QUERY = AUTH + "-QUERY";

    private static final String AUTH_ADD = AUTH + "-ADD";

    private static final String AUTH_EDIT = AUTH + "-EDIT";

    private static final String AUTH_DEL = AUTH + "-DEL";

    @Reference
    private ActivityService activityService;

    @Reference
    private StoreService storeService;

    @TypeField(type = ActivityStore.class, field = "store", description = "商户")
    public Store store(@Source ActivityStore source){
        return storeService.getStore(source.getStoreId());
    }

    @TypeField(type = Store.class, field = "isRelationActivity", description = "是否关联了当前活动内容")
    public Boolean isRelationActivity(@Source Store source, @NonNull Integer activityId){
        return activityService.isRelationActivity(activityId,source.getStoreId());
    }

    @QueryField(field = "activityStore", description = "商圈活动内容关联商户详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "商圈活动内容关联商户详情")
    public ActivityStore get(@NonNull Integer id, AdminMemberAuthority user){
        user.assertAuth(AUTH_GET);
        return activityService.getActivityStore(id);
    }

    @QueryField(field = "activityStoreList", description = "商圈活动内容关联商户列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "商圈活动内容关联商户列表")
    public Pagination<ActivityStore> query(String storeName, @NonNull Integer activityId, @NonNull Page page, AdminMemberAuthority user){
        user.assertAuth(AUTH_QUERY);
        return activityService.queryActivityStore(storeName,activityId,page);
    }

    @MutationField(field = "addActivityStore", description = "添加商圈活动内容关联商户")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商圈活动内容关联商户")
    public Boolean add(@NonNull Integer activityId, @NonNull List<Integer> storeIdList, AdminMemberAuthority user){
        user.assertAuth(AUTH_ADD);
        activityService.addActivityStore(activityId,storeIdList,user.userName());
        return true;
    }

    @MutationField(field = "editActivityStore", description = "编辑商圈活动内容关联商户")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商圈活动内容关联商户")
    public ActivityStore edit(@NonNull Integer id, Integer weight, AdminMemberAuthority user){
        user.assertAuth(AUTH_EDIT);
        ActivityStore old = activityService.getActivityStore(id);
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "活动内容关联商户不存在");
        old.setWeight(weight);
        return activityService.editActivityStore(old,user.userName());
    }

    @MutationField(field = "delActivityStore", description = "删除商圈活动内容关联商户")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商圈活动内容关联商户")
    public Boolean delete(@NonNull Integer id, AdminMemberAuthority user){
        user.assertAuth(AUTH_DEL);
        ActivityStore old = activityService.getActivityStore(id);
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "活动内容关联商户不存在");
        activityService.deleteActivityStore(old,user.userName());
        return true;
    }
}
